package com.dcshop.demo.controller;

import com.alibaba.fastjson.JSON;
import com.dcshop.demo.contract.ContractUtils;
import com.dcshop.demo.contract.ProductContract;
import com.dcshop.demo.dto.ProductContractDTO;
import com.github.ontio.OntSdk;
import com.github.ontio.account.Account;
import com.github.ontio.smartcontract.neovm.abi.AbiInfo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Map;

public class ProofController extends AbsController {

    /**
     * 产品链上查询
     *
     * @param reqMap
     * @return
     */
    @PostMapping("/proofProduct")
    public Boolean proofProduct(@RequestBody Map<String, Object> reqMap) {
        String productid = reqMap.get("productid").toString();
        if (StringUtils.isEmpty(productid)) {
            return false;
        }
        OntSdk ontSdk = null;
        try {
            ontSdk = getOntSdk();
            Account payAccount = getPlatformAccount(ontSdk);
            //TODO 客户在登陆系统的时候会取到productOwerAccount，测试先用payAccount
            Account productOwerAccount = payAccount;

            //调用资产评估证明 智能合约
            AbiInfo info = JSON.parseObject(ContractUtils.ABI, AbiInfo.class);
            //数据上链
            ProductContractDTO productContractDTO = new ProductContractDTO();
            productContractDTO.setOntSdk(ontSdk);
            productContractDTO.setAccount(productOwerAccount);
            productContractDTO.setPayAccount(payAccount);
            productContractDTO.setInfo(info);
            productContractDTO.setProductid(productid);
            Object res = ProductContract.addProducts(productContractDTO);

            //TODO 返回处理，页面展示

            return true;


        } catch (Exception e) {
            e.getMessage();
            return false;
        }

    }


}
